/*
 * Created on 12/07/08
 *
 */
package util.math;

/**
 * @author S.A.M.G.
 * @since 07/08.
 */
public class PointCalculator {
	/**
	 * This method allows to calcule the Euclidean distance
	 * between the two given points.
	 * @param a
	 * @param b
	 * @return euclidean distance.
	 */
	public static double getEuclideanDistance(int[]a,int[]b){
		if(isPossibleToOperate(a.length,b.length)){
			return Math.sqrt(getSquareError(a, b));
		}else{
			return -1;
		}
	}
	public static double getEuclideanDistance(short[]a,short[]b){
		int[]tmpA = new int[a.length];
		int[]tmpB = new int[b.length];
		for (int i = 0; i < tmpA.length; i++) {
			tmpA[i] = a[i];
		}
		for (int i = 0; i < tmpB.length; i++) {
			tmpB[i] = b[i];
		}
		return PointCalculator.getEuclideanDistance(tmpA, tmpB);
	}
	public static double getEuclideanDistance(int a,int b){
		int[]tmpA = {a};
		int[]tmpB = {b};
		return PointCalculator.getEuclideanDistance(tmpA, tmpB);
	}
	
	/**
	 * This method allows to calculate the square error
	 * betweet the two given points.
	 * @param a
	 * @param b
	 * @return square error.
	 */
	public static double getSquareError(short[]a,short[]b){
		int[]tmpA = new int[a.length];
		int[]tmpB = new int[b.length];
		for (int i = 0; i < tmpA.length; i++) {
			tmpA[i] = a[i];
		}
		for (int i = 0; i < tmpB.length; i++) {
			tmpB[i] = b[i];
		}
		return PointCalculator.getSquareError(tmpA, tmpB);
	}
	public static double getSquareError(int[]a,int[]b){
		if(isPossibleToOperate(a.length,b.length)){
			double ans = 0;
			int length = a.length;
			for (int i = 0; i < length; i++) {
				ans += Math.pow(a[i]-b[i], 2);
			}
			return ans;
		}else{
			return -1;
		}
	}
	
	private static boolean isPossibleToOperate(int a,int b){
		return a==b;
	}
	
	public boolean isIntoRange(int value,int reference,int error){
		return (value>=(reference-error))&&(value<=(reference+error));
	}
}
